/**
 * Copyright (2017, ) Institute of Software, Chinese Academy of Sciences
 */

package cn.abcsys.devops.commons.utils;

import com.sun.org.apache.xpath.internal.operations.Bool;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author xuyuanjia2017@otcaix.iscsa.ac.cn
 * @date 2017/6/20 0020
 * say sth.
 */

public class StringUtils {

	public static Boolean checkPorts(String ports){
		if(ports == null)
			return true;
		if(ports.length() == 0 )
			return true;
		String regex = "(([0-9]){1,5};){1,20}";
		Pattern pa = Pattern.compile(regex);
		Matcher ma = pa.matcher(ports);
		//System.out.println(ma.matches());
		if(ma.matches()){
			String[] port = ports.split(";");
			for(int i = 0;i<port.length;i++){
				try {
					for(int j=i+1;j<port.length;j++){
						if(port[i].equals(port[j])){
							//System.out.println("good");
							return false;
						}
					}
					Integer tempI = Integer.valueOf(port[i]);
					//System.out.println(tempI);
					if(tempI<0 || tempI>65535)
						return false;
				}catch (Exception e){
					return false;
				}
			}
			return true;
		}
		return false;
	}

	public static Boolean checkMemory(String memory){
		if(memory == null || memory.length()<1)
			return false;
		try{
			Integer mem =Integer.valueOf(memory);
			if(mem >100 && mem<=16384){
				return true;
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return false;
	}

	public static Boolean checkCpu(String Cpu){
		Integer i;
		if(Cpu == null || Cpu.length()<1)
			return false;
		try{
			if(Cpu.lastIndexOf("m") == Cpu.length()-1){
				i = Integer.valueOf(Cpu.replace("m",""));
				if(i>=100&&i<1000)
					return true;
			}
			else{
				i = Integer.valueOf(Cpu);
				if(i>=1&&i<=16)
					return true;
			}

		}catch (Exception e){
			e.printStackTrace();
		}
		return false;
	}

	public static Boolean checkEnvList(String envListString){
		if(envListString == null)
			return true;
		if(envListString.length() == 0)
			return true;
		String regex = "([0-9a-zA-Z_/\\-]{1,20}:[0-9a-zA-Z_/:\\-]{1,100};){0,20}";
		Pattern pa = Pattern.compile(regex);
		Matcher ma = pa.matcher(envListString);
		//System.out.println(ma.matches());
		if(ma.matches()){
			return checkReplication(envListString.split(";"));
		}
		return false;
	}

	private static Boolean checkReplication(String[] list){
		for(int i=0;i<list.length;i++){
			for(int j=i+1;j<list.length;j++){
				if(list[i].equals(list[j])){
					return false;
				}
			}
		}
		return  true;
	}

	public static  Boolean checkVolumesList(String vol){
		if(vol == null)
			return true;
		if(vol.length() == 0)
			return true;
		String regex = "([a-zA-Z]{1,20}:[0-9a-zA-Z\\-]{1,20}:[0-9a-zA-Z_/\\-\\.]{1,100}:[a-zA-Z_\\-]{1,20};){0,10}";
		Pattern pa = Pattern.compile(regex);
		Matcher ma = pa.matcher(vol);
		//.out.println(ma.matches());
		if(ma.matches()){
			return checkReplication(vol.split(";"));
		}
		return false;
	}

	public static Boolean checkImageTag(String image,String tag){
		if(image == null)
			return false;
		if(tag !=null && tag.length()>20)
			return false;
		if(image !=null && image.length()>100)
			return false;
		String regex = "([0-9a-zA-Z_/:_\\-\\.]){1,100}";
		Pattern pa = Pattern.compile(regex);
		Matcher ma = pa.matcher(image);
		//System.out.println(ma.matches());
		return ma.matches();
	}

	public static boolean checkString(String name,CheckPolicy cp) {
		if(name == null)
			return false;
		String regex = "";
		switch (cp){
			case STRING_ORIDNARY:
				regex = "(?!^\\d+$)(^[a-z])[0-9a-zA-Z_]{3,19}";
				break;
			case STRING_STRICT:
				regex = "(?!^\\d+$)(^[a-z])[0-9a-z]{3,19}";
				break;
			default:
				regex = "(?!^\\d+$)(^[a-z])[0-9a-zA-Z_/:\\-]{3,49}";
				break;
		}

		Pattern pa = Pattern.compile(regex);
		Matcher ma = pa.matcher(name);
		//System.out.println(ma.matches());
		return ma.matches();
	}


	public static void main(String[] args) {
		//System.out.println(checkCpu("17m"));
		//System.out.println(checkEnvList("XYJ_HOME:/opt/path/;XYJ_HOME:/opt/path/;"));
		//checkImageTag("registry.cn-hangzhou.aliyuncs.com/xuyuanjia/docker","");
		//System.out.println(checkMemory("16385"));
		//System.out.println(checkPorts("8080;8080;"));
		System.out.println(checkVolumesList("local:local-name:/root/apache-tomcat-9.0.0.M21/logs:localFileSystem;"));
		//System.out.println(checkString("a011",CheckPolicy.STRING_STRICT));
	}
	
}
